const { sqlite3 } = require('../../mysql')

// 获取视频列表
const avmooList = async (ctx) => {
  console.log('ctx.query:', ctx.query)
  let formData = ctx.query
  let offset = (formData.currentPage - 1) * formData.pageSize
  let limit = formData.pageSize
  // let keyword = Object()
  // keyword.title = formData.title
  // keyword.id = formData.id
  let stime = new Date().getTime()
  if (keyword) {
    let { id, title } = formData
    console.log('id, title:', id, title)
    var fieldName = id ? 'id' : 'title'
    var keyword = id || title
    data = await sqlite3('avmoo_list')
      .where(fieldName, 'LIKE', '%' + keyword + '%')
      .orderBy('id', 'desc')
      .select()
      .limit(formData.limit)
      .offset(offset)
    total = data.length
  } else {
    totalData = await sqlite3('avmoo_list').count({ total: '*' })
    data = await sqlite3('avmoo_list').orderBy('id', 'desc').select().limit(limit).offset(offset)
    total = totalData[0].total
    console.log('pageSize:', formData.currentPage, 'currentPage:', limit)
  }
  let etime = new Date().getTime()
  console.log(`response time: ${etime - stime}ms`)
  if (data) {
    ctx.body = {
      success: true,
      data: {
        list: data,
        total,
        pageSize: Number(limit),
        currentPage: Number(formData.currentPage)
      },
    }
  } else {
    ctx.body = {
      success: false
    }
  }
}

// 添加
const avmooCreate = async (ctx) => {
  console.log('ctx:', ctx.request.body)
  let formData = ctx.request.body
  if (formData.id == null) {
    lastId = await sqlite3('avmoo_list').orderBy('id', 'desc').select().limit(1)
    formData.id = lastId[0].id + 1
  }

  console.log(formData.id)
  const data = await sqlite3('avmoo_list').insert(
    formData
  )
  console.log(data)
  if (data) {
    ctx.body = {
      success: 200,
      data: "success",
    }
  } else {
    ctx.body = {
      success: false
    }
  }
}

// 添加列表
const avmooCreateList = async (ctx) => {
  console.log('ctx:', ctx.request.body.content)
  // console.log('ctx:', ctx.request.body.content[1])
  // let formData = ctx.request.body.content
  // console.log('formData:', formData.content)
  // console.log('formData.typeof:', typeof(formData))
  
  // for (let i=0, len=formData.length; i<len; i++) {
  //   let data = formData[i]
  //   console.log('data:', data.data)
  // }
  // let formData = ctx.request.body
  // if (formData.id == null) {
  //   lastId = await sqlite3('avmoo_list').orderBy('id', 'desc').select().limit(1)
  //   formData.id = lastId[0].id + 1
  // }

  // console.log(formData.id)
  // const data = await sqlite3('avmoo_list').insert(
  //   formData
  // )
  // console.log(data)
  if (ctx.request.body) {
    ctx.body = {
      success: 200,
      data: "success",
    }
  } else {
    ctx.body = {
      success: false
    }
  }
}

// 修改
const avmooUpdate = async (ctx) => {
  console.log('ctx:', ctx.request.body)
  let formData = ctx.request.body
  const data = await sqlite3('avmoo_list')
    .where('id', formData.id)
    .update(formData)
  // console.log(data)
  if (data) {
    ctx.body = {
      success: 200,
      data: "success"
    }
  } else {
    ctx.body = {
      success: false
    }
  }
}

// 删除
const avmooDelete = async (ctx) => {
  console.log('ctx:', ctx.request.body)
  let formData = ctx.request.body
  const data = await sqlite3('avmoo_list')
    .where('id', formData.id).del()
  console.log(data)
  if (data) {
    ctx.body = {
      code: 200,
      data: "success"
    }
  } else {
    ctx.body = {
      code: false
    }
  }
}

// 获取role列表
const roleList = async (ctx) => {
  console.log('ctx.query:', ctx.query)
  let formData = ctx.query
  let offset = (formData.currentPage - 1) * formData.pageSize
  let limit = formData.pageSize
  let keyword = formData.title
  let stime = new Date().getTime()
  if (keyword) {
    data = await sqlite3('role')
      .where('title', 'LIKE', '%' + keyword + '%')
      .orderBy('id', 'desc')
      .select()
      .limit(formData.limit)
      .offset(offset)
    total = data.length
  } else {
    totalData = await sqlite3('role').count({ total: '*' })
    data = await sqlite3('role').orderBy('id', 'desc').select().limit(limit).offset(offset)
    total = totalData[0].total
    console.log('pageSize:', formData.currentPage, 'currentPage:', limit)
  }
  let etime = new Date().getTime()
  console.log(`response time: ${etime - stime}ms`)
  if (data) {
    ctx.body = {
      success: true,
      data: {
        list: data,
        total,
        pageSize: Number(limit || 15),
        currentPage: Number(formData.currentPage || 1)
      },
    }
  } else {
    ctx.body = {
      success: false
    }
  }
}

module.exports = { avmooList, avmooCreate, avmooCreateList, avmooUpdate, avmooDelete, roleList }
